package cn.laike.modules.dev.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import cn.laike.common.validator.ValidatorUtils;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.laike.modules.dev.entity.HotelEntity;
import cn.laike.modules.dev.entity.PrinterEntity;
import cn.laike.modules.dev.service.HotelService;
import cn.laike.modules.dev.service.PrinterService;
import cn.laike.modules.dev.utils.PrinterUtils;
import net.sf.json.JSONObject;
import cn.laike.common.utils.PageUtils;
import cn.laike.common.utils.R;



/**
 * 打印机配置
 *
 * @author ziye
 * @email 524748907@qq.com
 * @date 2019-02-26 09:40:59
 */
@RestController
@RequestMapping("dev/printer")
public class PrinterController {
    @Autowired
    private PrinterService printerService;
    
    @Autowired
	private HotelService hotelService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("dev:printer:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = printerService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("dev:printer:info")
    public R info(@PathVariable("id") Integer id){
        PrinterEntity printer = printerService.selectById(id);

        return R.ok().put("printer", printer);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("dev:printer:save")
    public R save(@RequestBody PrinterEntity printer){
    	HotelEntity hotel = hotelService.selectById(printer.getHotelId());
    	if(hotel == null) {
    		return R.error("请到'酒店列表'->'房型管理'添加！");
    	}
    	printer.setHotelId(hotel.getId());
    	printer.setHotelName(hotel.getName());
    	
    	printer.setUid(hotel.getUid());
    	printer.setCreateTime(new Date());
    	
    	
    	String snlist = printer.getSn() + "#"+printer.getSkey()+"#"+printer.getName()+"#" + printer.getPhonenum();
		String method = PrinterUtils.addprinter(snlist);
		//{"msg":"ok","ret":0,"data":{"ok":["918508249#yvqt88g2#A001#"],"no":[]},"serverExecutedTime":7}
		//{"msg":"ok","ret":0,"data":{"ok":[],"no":["918508249#yvqt88g2#A001# \uff08\u9519\u8bef\uff1a\u5df2\u88ab\u6dfb\u52a0\u8fc7\uff09"]},"serverExecutedTime":5}
		//解析相应内容（转换成json对象）
        JSONObject json = JSONObject.fromObject(method);
		json = json.getJSONObject("data");
        if(json.getJSONArray("ok").size() > 0) {
        	System.out.println(method);
            printerService.insert(printer);
            return R.ok();
        }else {
        	if(json.getJSONArray("no").getString(0).contains("已被添加过")) {
        		PrinterEntity p = printerService.queryPrinterList(printer.getSn());
        		if(p == null) {
        			PrinterUtils.editprinter(printer.getSn(), printer.getName(), StringUtils.isEmpty(printer.getPhonenum()) ? "-" : printer.getPhonenum());
            		System.out.println(method);
                    printerService.insert(printer);
                    return R.ok();
        		}else {
        			 return R.error("添加失败,请先删除( "+p.getHotelName()+")下的打印机");
        		}
        		
                
        	}else {
        		return R.error(json.getJSONArray("no").getString(0));
        	}
        }
		
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("dev:printer:update")
    public R update(@RequestBody PrinterEntity printer){
        ValidatorUtils.validateEntity(printer);
        HotelEntity hotel = hotelService.selectById(printer.getHotelId());
    	if(hotel == null) {
    		return R.error("请到'酒店列表'->'房型管理'添加！");
    	}
    	printer.setHotelName(hotel.getName());
    	
		String method = PrinterUtils.editprinter(printer.getSn(), printer.getName(), StringUtils.isEmpty(printer.getPhonenum()) ? "-" : printer.getPhonenum());
		//{"msg":"ok","ret":0,"data":{"ok":["918508249#yvqt88g2#A001#"],"no":[]},"serverExecutedTime":7}
		//{"msg":"ok","ret":0,"data":{"ok":[],"no":["918508249#yvqt88g2#A001# \uff08\u9519\u8bef\uff1a\u5df2\u88ab\u6dfb\u52a0\u8fc7\uff09"]},"serverExecutedTime":5}
		//解析相应内容（转换成json对象）
        JSONObject json = JSONObject.fromObject(method);
        if(json.getInt("ret") == 0) {
        	System.out.println(method);
        	printerService.updateAllColumnById(printer);//全部更新
            return R.ok();
        }else {
        	return R.error(json.getString("msg"));
        }
       
        
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dev:printer:delete")
    public R delete(@RequestBody Integer[] ids){
//    	for (Integer id : ids) {
//    		PrinterEntity printer = printerService.selectById(id);
//    		String method = PrinterUtils.delprinter(printer.getSn());
//    		//{"msg":"ok","ret":0,"data":{"ok":["918508249#yvqt88g2#A001#"],"no":[]},"serverExecutedTime":7}
//    		//{"msg":"ok","ret":0,"data":{"ok":[],"no":["918508249#yvqt88g2#A001# \uff08\u9519\u8bef\uff1a\u5df2\u88ab\u6dfb\u52a0\u8fc7\uff09"]},"serverExecutedTime":5}
//    		//解析相应内容（转换成json对象）
//            JSONObject json = JSONObject.fromObject(method);
//            if(json.getInt("ret") == 0) {
//            	System.out.println(method);
//            	printerService.deleteById(id);
//            }
//		}
//        return R.ok();
    	
    	printerService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

}
